function saveEdit(){
	var str = "[";
	var flag = true;
	var positions = {};
	var hasPrimary = false;
	$('form').find('ul[index]').each(function(index,doc){
		var org = $(doc).find('input[dataPicker="value"]').val();
		var position = $(doc).find('select[name="position"]').val();
		var jobLevel = $(doc).find('select[name="jobLevel"]').val();
		var primary = $(doc).find('input[name="primary"]')[0].checked;
		var id = $(doc).find('input[name="id"]').val();
		if(position==null || position == ''){
			alert('任职职位不能为空');
			flag = false;
			return false;
		}else{
			if(positions[position]){
				alert('存在重复的任职职位,请确认');
				flag = false;
				return false;
			}
			positions[position] = position;
			str += "{'position':{'id':'" + position + "'},";
		}
		if(jobLevel==null || jobLevel == ''){
			alert('任职职级不能为空');
			flag = false;
			return false;
		}else{
			str += "'jobLevel':{'id':'" + jobLevel + "'},";
		}
		if(primary==true || primary=='checked'){
			hasPrimary = true;
			str += "'primary':" + "true,";
		}else{
			str += "'primary':" + "false,";
		}
		if(id){
			str += "'id':'"+id+"'";
		}else{
			str += "'id':''";
		}
		str +="},";
	});
	if(str!='['){
		str = str.substring(0,str.length - 1);
	}
	str += "]";
	if(!hasPrimary){
		alert('没有主要任职信息');
		return false;
	}
	if(flag){
		$('#positionJson').val(str);
		$("form").submit();
	}
	return false;
}
function saveAdd(){
	saveEdit();
}

function addPersonPosition(){
	var index = 0;
	$('form').find('ul[index]').each(function(){
		var idx = parseInt($(this).attr('index'));
		if(idx >= index){
			index = idx+1;
		}
	});
	$('<ul index="'+index+'">'
	+'	<li class="field_label_li">组织：</li>'
	+'	<li style="width:120px;text-align:left;">'
	+'  	<input type="hidden" name="id" />'
	+'		<div class="f7" id="org_'+index+'" dataPickerUrl="'+getPath()+'/basedata/org/orgDataPicker" width="750px" height="500px" onchange="changeOrg" title="组织">'
	+'    		<input dataPicker="value"  name="org.id" type="hidden" readOnly="readOnly" />'
	+'    		<input dataPicker="name" ondblclick="openDataPicker(\'org_'+index+'\')"name="org.name" type="text"/>'
	+'			<strong onclick="clearDataPicker(\'org_'+index+'\')"></strong>'
	+'			<span class="p_hov" onclick="openDataPicker(\'org_'+index+'\')"></span>'
	+'		</div>'
	+'	</li>'
	+'	<li class="field_label_li">职位：</li>'
	+'	<li style="width:120px;text-align:left;">'
	+'		<select name="position" style="width:110px">'
	+'		</select>'
	+'	</li>'
	+'	<li class="field_label_li">职级：</li>'
	+'	<li style="width:120px;text-align:left;">'
	+'		<select name="jobLevel" style="width:110px">'
	+'		</select>'
	+'	</li>'
	+'	<li style="width:100px;text-align:left;">'
	+'		<label><input name="primary" type="checkbox" value="true"/>主要职位</label>'
	+'	</li>'
	+'</ul>').appendTo('form');
	initEvents();
}

function changeOrg(oldValue,newValue,doc){
	var ul = $(doc).parent();
	while(ul.attr('index')==null){
		ul = $(ul).parent();
	}
	var p = $(ul).find('select[name="position"]');
	p.val(null);
	p.html('');
	if(newValue){
		$.post(getPath()+'/basedata/position/getByOrg',{org:newValue.id},function(res){
			if(res && res.length > 0){
				for(var i = 0; i < res.length; i++){
					$('<option value="'+res[i].id+'">'+res[i].name+'</option>').appendTo(p);
				}
				p.trigger('change');
			}
		},'json');
	}
}

$(document).ready(function(){
	initEvents();
});

function initEvents(){
	$('form').find('select[name="position"]').each(function(){
		var d = this;
		$(d).unbind('change');
		$(d).bind('change',function(){
			var ul = $(d).parent();
			while(ul.attr('index')==null){
				ul = $(ul).parent();
			}
			var jl = $(ul).find('select[name="jobLevel"]');
			jl.val(null);
			jl.html('');
			if($(d).val()){
				$.post(getPath()+'/basedata/position/getJobLevelByPosition',{position:$(d).val()},function(res){
					if(res && res.length > 0){
						for(var i = 0; i < res.length; i++){
							$('<option value="'+res[i].id+'">'+res[i].name+'</option>').appendTo(jl);
						}
					}
				},'json');
			}
		});
	});
	$('input[name="primary"]').each(function(){
		var cbx = this;
		$(cbx).unbind('change');
		$(cbx).bind('change',function(){
			if($(cbx).attr('checked')==true || $(cbx).attr('checked')=='checked'){
				$('input[name="primary"]').each(function(){
					if(this!=cbx){
						$(this).removeAttr('checked');
					}
				});
			}
		});
	});
}